
/**
 * Island
 * Template of an instance of island
 **/
var Island = function() {
	
	this.init = function(axis, angle) {
		// log('*** INIT ISLAND ***');
		// Set dimension of island according to the sprite
		this.h = 175;
		this.w = 125;

		// Place island on the axis trajectory
		this.x = axis.x + Math.sin(angle) * (axis.w/2) - this.w/2;
		this.y = axis.y + Math.cos(angle) * (axis.h/2) - this.h/2;

		// Load an instance of the image
		this.sprite = IM.getInstance('img/island');

		this.alpha = 1;
	}

	this.update = function() {
		// Start drag island
		if(input.mouse.click == true) {
			this.offset = input.drag(this);
		}

		// Update island position according to drag action
		if(input.mouse.drag == true) {
		    this.x = input.mouse.x - this.offset.x;
		    this.y = input.mouse.y - this.offset.y;
		}

		this.alpha = 1 - distance(this, {x:WIDTH/2,y:HEIGHT/2})/1000;
		if(this.alpha > .6) {
			this.alpha = 1
		}
		else if(this.alpha < 0) {
			this.alpha = 0
		}
	}

	this.render = function() {
		// Save canvas
		CTX.save();
		// Apply island's alpha to the canvas
		CTX.globalAlpha = this.alpha;

		// Draw island
		IM.drawImage(CTX, this.sprite, this.x,this.y);
		// Draw anchor
		// drawRect(CTX,"#F00",this.x,this.y,5,5);

		// Restore canvas with normal settings
		CTX.restore();
	}

};